#include "PAP_defs.h"
#include "ClockSource_QE.h"

/*
#define SCI_4800BAUD 104
#define SCI_9600BAUD 52
#define SCI_19200BAUD 26
#define SCI_38400BAUD 13
*/

#ifdef ICS_DCO_MAX_FREQ
#define SCI_50000BAUD 25
#define SCI_62500BAUD 20
#define SCI_83333BAUD 15
#define SCI_125000BAUD 10
#define SCI_250000BAUD 5
#else
#define SCI_50000BAUD   20
#define SCI_62500BAUD   16
#define SCI_83333BAUD   12
#define SCI_125000BAUD  8
#endif

/* SCI2 CONFIGURATION PART 1: define the features that are needed */
    
    //#define SCI2_LOOP_MODE_SELECT
    //#define SCI2_WAIT_INACTIVE
    //#define SCI2_SINGLE_WIRE_SCI
    //#define SCI2_MODE_9BIT
    //#define SCI2_ADDRESS_MARK_WAKEUP
    //#define SCI2_IDLE_LINE_STOP_BIT
    //#define SCI2_PARITY_ENABLE
    //#define SCI2_PARITY_ODD

/* SCI2 CONFIGURATION PART 1 ends here*/

/* SCI2 CONFIGURATION PART 2: define the features that are needed */
    
    //#define SCI2_TX_INT_ENABLED
    //#define SCI2_TX_COMP_INT_ENABLED
    //#define SCI2_RX_INT_ENABLED
    //#define SCI2_IDLE_LINE_INT_ENABLED
    #define SCI2_TX_ENABLED
    #define SCI2_RX_ENABLED
    //#define SCI2_RX_STANDBY
    //#define SCI2_SEND_BREAK

/* SCI2 CONFIGURATION PART 2 ends here*/

/* DO NOT MODIFY */
#ifdef SCI2_LOOP_MODE_SELECT
    #define SCI2_LOOP 0x80
#else
    #define SCI2_LOOP 0x00
#endif

#ifdef SCI2_WAIT_INACTIVE
    #define SCI2_WAIT 0x40
#else 
    #define SCI2_WAIT 0x00
#endif 

#ifdef SCI2_SINGLE_WIRE_SCI
    #define SCI2_DUPLEX 0x20
#else
    #define SCI2_DUPLEX 0x00
#endif

#ifdef SCI2_MODE_9BIT
    #define SCI2_BIT_MODE 0x10
#else
    #define SCI2_BIT_MODE 0x00
#endif

#ifdef SCI2_ADDRESS_MARK_WAKEUP
    #define SCI2_WAKEUP 0x80
#else
    #define SCI2_WAKEUP 0x00
#endif

#ifdef SCI2_IDLE_LINE_STOP_BIT
    #define SCI2_IDLE_LINE_TYPE 0x40
#else
    #define SCI2_IDLE_LINE_TYPE 0x00
#endif

#ifdef SCI2_PARITY_ENABLE
    #define SCI2_PARITY 0x20
#else
    #define SCI2_PARITY 0x00
#endif

#ifdef SCI2_PARITY_ODD
    #define SCI2_PARITY_TYPE 0x01
#else
    #define SCI2_PARITY_TYPE 0x00
#endif

#define SCI2_CONTROL_REG1  ( SCI2_LOOP | SCI2_WAIT | SCI2_DUPLEX | SCI2_BIT_MODE   \
                           | SCI2_WAKEUP | SCI2_IDLE_LINE_TYPE | SCI2_PARITY       \
                           | SCI2_PARITY_TYPE ) 

#ifdef SCI2_TX_INT_ENABLED
    #define SCI2_TX_INT 0x80
#else
    #define SCI2_TX_INT 0x00
#endif

#ifdef SCI2_TX_COMP_INT_ENABLED
    #define SCI2_TX_COMP_INT 0x40
#else 
    #define SCI2_TX_COMP_INT 0x00
#endif 

#ifdef SCI2_RX_INT_ENABLED
    #define SCI2_RX_INT 0x20
#else
    #define SCI2_RX_INT 0x00
#endif

#ifdef SCI2_IDLE_LINE_INT_ENABLED
    #define SCI2_IDLE_LINE_INT 0x10
#else
    #define SCI2_IDLE_LINE_INT 0x00
#endif

#ifdef SCI2_TX_ENABLED
    #define SCI2_TX_EN 0x08
#else
    #define SCI2_TX_EN 0x00
#endif

#ifdef SCI2_RX_ENABLED
    #define SCI2_RX_EN 0x04
#else
    #define SCI2_RX_EN 0x00
#endif

#ifdef SCI2_RX_STANDBY
    #define SCI2_RX_WAITING 0x02
#else
    #define SCI2_RX_WAITING 0x00
#endif

#ifdef SCI2_SEND_BREAK
    #define SCI2_BREAK_QUEUE 0x01
#else
    #define SCI2_BREAK_QUEUE 0x00
#endif

#define SCI2_CONTROL_REG2    (SCI2_TX_INT | SCI2_TX_COMP_INT | SCI2_RX_INT  \
                            | SCI2_IDLE_LINE_INT | SCI2_TX_EN | SCI2_RX_EN  \
                            | SCI2_RX_WAITING | SCI2_BREAK_QUEUE)
                            
#define SCI2_CONTROL_REG3 0x00

#define SCI1_CONTROL_REG1 0x00
#define SCI1_CONTROL_REG2 0x00
#define SCI1_CONTROL_REG3 0x00

/* DO NOT MODIFY ENDS HERE */

void vfnSCI_SetBaudRate (uint8 u8channel, uint16 u16baudrate);

void vfnSCI_Init (uint8 u8channel);

/* Channel 1 */

void vfnSCI1_SendByte (uint8 u8data);

uint8 u8SCI1_TxBufferEmpty (void);

uint8 u8SCI1_TxComplete (void);

uint8 u8SCI1_CheckOverrun (void);

uint8 u8SCI1_ReadByte (void);

uint8 u8SCI1_RxPending (void);

/* Channel 2 */

void vfnSCI2_SendByte (uint8 u8data);

uint8 u8SCI2_TxBufferEmpty (void);

uint8 u8SCI2_TxComplete (void);

uint8 u8SCI2_CheckOverrun (void);

uint8 u8SCI2_ReadByte (void);

uint8 u8SCI2_RxPending (void);



